package sjms.create.singleton;

public class Singleton_5 {
    public static void main(String[] args) {

    }
}

/**
 * 双重检查：
 *  优点：懒加载、线程安全
 */

class Singleton5{

    // 用volatile修饰保证其内存可见性
    private static volatile Singleton5 instance;

    private Singleton5 (){}

    public static Singleton5 getInstance(){
        if(instance == null){
            synchronized (Singleton5.class){
                if(instance == null){
                    instance = new Singleton5();
                }
            }
        }
        return instance;
    }
}
